//
//  STGroupAnimationViewController.m
//  KeyframeAndGroupAnimation
//
//  Created by Stan on 2017-02-19.
//  Copyright © 2017 stan. All rights reserved.
//

#import "STGroupAnimationViewController.h"

@interface STGroupAnimationViewController ()
@property(strong,nonatomic)CALayer *orangeLayer;

@end

@implementation STGroupAnimationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置layer的position
    self.orangeLayer.position = self.view.center;
    //将layer添加到view的layer上。就类似自定义的view要添加到controller的view上才能显示一样
    [self.view.layer addSublayer:self.orangeLayer];
    //为layer添加动画
        [self.orangeLayer addAnimation:[self groupAni] forKey:nil];
    
}



- (CAAnimationGroup *)groupAni{
    //        实例化一个组动画对象
        CAAnimationGroup *groupAni = [[CAAnimationGroup alloc] init];
    
    //        创建旋转的动画
        CABasicAnimation *basicRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basicRotation.toValue = @(M_PI * 2);
    
    //        创建缩放的动画
        CABasicAnimation *basicScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    basicScale.toValue = @(0.2);
    
    //        创建按照路径移动的动画
        CAKeyframeAnimation *keyFrameAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:150 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
    
    keyFrameAni.path = arcPath.CGPath;
    keyFrameAni.calculationMode = kCAAnimationPaced;
    keyFrameAni.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    //        将旋转、缩放、移动的动画添加到组动画中
    groupAni.animations = @[basicRotation,basicScale,keyFrameAni];
    //        组动画重复次数
        groupAni.repeatCount = CGFLOAT_MAX;
    //        组动画时长
        groupAni.duration = 2;
    
    return groupAni;
}


//懒加载
- (CALayer *)orangeLayer{
    if (!_orangeLayer) {
        _orangeLayer = [[CALayer alloc] init];
        _orangeLayer.bounds = CGRectMake(0, 0, 150, 150);
        
        _orangeLayer.backgroundColor = [UIColor orangeColor].CGColor;
    }
    return _orangeLayer;
}
@end
